/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package com.luxoft.corporate.blog.db;

import com.luxoft.corporate.blog.db.entities.Topic;
import com.luxoft.corporate.blog.db.entities.User;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.LinkedList;
import java.util.List;

/**
 *
 * @author Egor
 */
public final class ControllerFactory {

    private ControllerFactory() {
    }

    public static Controller<User> createUserController(Connection connection) {

        return new UserController(connection);

    }

    
    public static Controller<Topic> createTopicController(Connection connection){
        return new TopicController(connection);
    }
    
    private static class UserController implements Controller<User> {

        private Connection connection;

        public UserController(Connection connection) {
            this.connection = connection;
        }

        @Override
        public List<User> getData(Query selectQuery) throws SQLException {
            List<User> users = new LinkedList<User>();
            try (Statement getDataStatement = connection.createStatement()) {
                try (ResultSet resultSet = getDataStatement.executeQuery(selectQuery.prepareQuery())) {
                    while (resultSet.next()) {
                        int u_id = resultSet.getInt(1);
                        String u_nick = resultSet.getString(2);
                        String u_password = resultSet.getString(3);
                        boolean u_IsAuthor = resultSet.getBoolean(4);

                        users.add(new User(u_id, u_password, u_nick, u_IsAuthor));

                    }
                }
            }
            return users;
        }

        @Override
        public User addData(Query insetQuery) throws SQLException {
            User user = new User();
            try (Statement addStatement = connection.createStatement()) {
                addStatement.executeUpdate(insetQuery.prepareQuery());

            }
            return user;
        }

        @Override
        public User removeData(Query deleteQuery) throws SQLException {
            User user = new User();
            try (Statement addStatement = connection.createStatement()) {
                try (ResultSet result = addStatement.executeQuery(deleteQuery.prepareQuery())) {
                    System.out.println(result.getInt(1));
                }

            }
            return user;
        }

        @Override
        public User modifyData(Query updateQuery) throws SQLException {
            User user = new User();
            try (Statement addStatement = connection.createStatement()) {
                try (ResultSet result = addStatement.executeQuery(updateQuery.prepareQuery())) {
                    System.out.println(result.getInt(1));
                }

            }
            return user;

        }

    }

    private static class TopicController implements Controller<Topic> {

        private Connection connection;

        public TopicController(Connection connection) {
            this.connection = connection;
        }

        @Override
        public List<Topic> getData(Query selectQuery) throws SQLException {

            return null;
        }

        @Override
        public Topic addData(Query insetQuery) throws SQLException {
            try (Statement statement = connection.createStatement()) {
                statement.executeUpdate(insetQuery.prepareQuery());
            }
            return null;
        }

        @Override
        public Topic removeData(Query deleteQuery) throws SQLException {
            return null;
        }

        @Override
        public Topic modifyData(Query updateQuery) throws SQLException {
            return null;
        }

    }

}
